using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Wojna.ServiceContracts;
using System.Configuration;

namespace Wojna.ServiceImplementation
{
    [ServiceBehavior(Name = "Play", Namespace = "http://Wojna.ServiceContracts/2007/11", 
        ConcurrencyMode = ConcurrencyMode.Single, 
        InstanceContextMode = InstanceContextMode.PerCall)]
    public class Play : IPlay
    {
        private static List<IPlayCallback> playerList = new List<IPlayCallback>();

        public Play() { }
        #region IPlay Members

        public void JoinGame(string nick)
        {
            IPlayCallback player = OperationContext.Current.GetCallbackChannel<IPlayCallback>();
            if (!playerList.Contains(player))
            {
                playerList.Add(player);
            }
            playerList.ForEach(delegate(IPlayCallback callback)
            { callback.NotifyPlayerJoinedGame(nick); });
        }

        public void LeaveGame(string nick)
        {
            IPlayCallback player = OperationContext.Current.GetCallbackChannel<IPlayCallback>();
            if (playerList.Contains(player))
            {
                playerList.Remove(player);
            }
            playerList.ForEach(delegate(IPlayCallback callback)
            { callback.NotifyPlayerLeftGame(nick); });
        }

        public void PolozKarte(Wojna.DataContracts.Karta karta)
        {
            IPlayCallback player = OperationContext.Current.GetCallbackChannel<IPlayCallback>();
            playerList.ForEach(delegate(IPlayCallback callback)
            { callback.NotifyWhatHappend(karta); });
        }

        #endregion
    }
}
